package com.egdigital.allzic.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

/**
 * @author julien
 * @version 1.0
 * @since 1.0
 */
public class GPSManager implements android.location.LocationListener {

	protected static final String	TAG								= GPSManager.class.getSimpleName();
	protected Context				mContext;
	protected static GPSManager		mInstance;
	protected static final long		MIN_DISTANCE_CHANGE_FOR_UPDATES	= 0;								// distance before update in m
	protected static final long		MIN_TIME_BW_UPDATES				= 1000 * 30 * 1;					// time before update in milli
	protected LocationManager		mLocationManager;
	boolean							mIsGPSEnabled					= false;
	boolean							mIsNetworkEnabled				= false;
	boolean							mCanGetLocation					= false;
	public Location					mLocation;
	double							mLatitude;
	double							mLongitude;

	public GPSManager() {
		mContext = null;
	}

	public static GPSManager getInstance() {
		if (mInstance == null) {
			mInstance = new GPSManager();
		}
		return mInstance;
	}

	public void setContext(Context context) {
		this.mContext = context;
	}

	/**
	 * @param context
	 * @return
	 */
	public Location getLocation(Context context) {
		mContext = context;
		try {
			mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
			mIsGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			mIsNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			if (!mIsGPSEnabled && !mIsNetworkEnabled) {
				// NO NETWORK PROVIDER ENABLED
			} else {
				this.mCanGetLocation = true;
				if (mIsNetworkEnabled) {
					mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
					Log.d(TAG, "Network");
					if (mLocationManager != null) {
						mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if (mLocation != null) {
							mLatitude = mLocation.getLatitude();
							mLongitude = mLocation.getLongitude();
						}
					}
				}
				// if GPS Enabled get lat/long using GPS Services
				if (mIsGPSEnabled) {
					if (mLocation == null) {
						mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
						Log.d(TAG, "GPS Enabled");
						if (mLocationManager != null) {
							mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							if (mLocation != null) {
								mLatitude = mLocation.getLatitude();
								mLongitude = mLocation.getLongitude();
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mLocation;
	}

	/**
 * 
 */
	public void stopUsingGPS() {
		if (mLocationManager != null) {
			mLocationManager.removeUpdates(GPSManager.this);
		}
	}

	public double getLatitude() {
		if (mLocation != null) {
			mLatitude = mLocation.getLatitude();
		}
		return mLatitude;
	}

	public double getLongitude() {
		if (mLocation != null) {
			mLongitude = mLocation.getLongitude();
		}
		return mLongitude;
	}

	/**
	 * @return
	 */
	public boolean canGetLocation() {
		return this.mCanGetLocation;
	}

	/**
	 * Function to show settings alert dialog On pressing Settings button will lauch Settings Options
	 * 
	 * @param title
	 * @param msg
	 */
	public void showSettingsAlert(String title, String msg) {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
		alertDialog.setTitle(title);
		alertDialog.setMessage(msg);
		// SETTING BUTTON
		alertDialog.setPositiveButton("Paramètres", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				mContext.startActivity(intent);
			}
		});
		// cancel button
		alertDialog.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		// show
		alertDialog.show();
	}

	@Override
	public void onLocationChanged(Location location) {
	}

	@Override
	public void onProviderDisabled(String provider) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}
}